package ru.javadevelop.vegetarian.fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import ru.javadevelop.vegetarian.R;
import ru.javadevelop.vegetarian.activities.RecipesInCatActivity;
import ru.javadevelop.vegetarian.adapters.CategoriesDataAdapter;
import ru.javadevelop.vegetarian.common.ExtApplication;
import ru.javadevelop.vegetarian.model.Category;

public class CategoriesFragment extends Fragment {
    private View view;
    private Context _context;
    private GridView grid_view;
    private CategoriesDataAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_categories, container, false);

        findControls();
        setListeners();
        fillControls();
        return view;
    }
    //******************************************************************************************************************
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _context = getActivity();

    }

    //******************************************************************************************************************
    public void findControls()
    {
        grid_view = (GridView)view.findViewById(R.id.grid_view);
    }
    //******************************************************************************************************************
    public void fillControls()
    {
        adapter = new CategoriesDataAdapter(_context, R.layout.items_category);
        grid_view.setAdapter(adapter);
    }
    //******************************************************************************************************************
    private void setListeners()
    {
        grid_view.setOnItemSelectedListener(new GridItemSelectedLstnr());
        grid_view.setOnItemClickListener(new GridItemClickLstnr());

    }
    //******************************************************************************************************************
    private class GridItemSelectedLstnr implements AdapterView.OnItemSelectedListener
    {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }
    //******************************************************************************************************************
    private class GridItemClickLstnr implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ExtApplication.Session.category = adapter.GetItem(position);
            Intent i = new Intent(_context, RecipesInCatActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            _context.startActivity(i);
        }
    }
    //******************************************************************************************************************
}
